/**
 * 
 */
package paymentService;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import constants.Constants;
import paymentEntity.CashEntity;
import paymentEntity.CreditCardEntity;

/**
 * @author Yokesh Kumar
 *
 */
public class PaymentService {

	private Connection connection;
	private PreparedStatement statement;

	public PaymentService(){
		try {
			Class.forName(Constants.JDBC_DRIVER);
			connection = DriverManager.getConnection(Constants.DB_URL,Constants.USER_NAME,Constants.PWD);
		} 
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	/**
	 * Stores the Payment details of the payment made by creditCard
	 * Stores in creditcard and payment tables
	 * @param creditCard
	 * @throws SQLException 
	 */
	public ResultSet insertIntoPayment(CreditCardEntity creditCard) throws SQLException{
		ResultSet rs = null;
		String query = "INSERT INTO payment values (null,?,?,?)";
		String query2 = "Select last_insert_id() as last_id from payment";
		try {
			connection.setAutoCommit(false);
			statement = connection.prepareStatement(query);
			statement.setDouble(1, creditCard.getPaymentAmount());
			statement.setString(2, creditCard.getPaidFor())	;
			statement.setString(3, creditCard.getPaymentType());
			statement.executeUpdate();
			statement = connection.prepareStatement(query2);
			rs = statement.executeQuery();
		} 
		catch (SQLException e1) {
			if(connection != null){
				System.out.println("transaction is rolled back");
				try {
					connection.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
					throw e;
				}
			}
			e1.printStackTrace();
			throw e1;
		}

		return rs;
	}

	public ResultSet insertIntoPayment(CashEntity cashEntity) throws SQLException{
		System.out.println("insert payment service : ");
		ResultSet rs = null;
		String query = "INSERT INTO payment values (null,?,?,?)";
		String query2 = "Select last_insert_id() as last_id from payment";
		try {
			connection.setAutoCommit(false);
			statement = connection.prepareStatement(query);
			statement.setDouble(1,cashEntity.getToatlAmount());
			statement.setString(2, cashEntity.getPaidFor());
			statement.setString(3, cashEntity.getPaymentType());
			statement.executeUpdate();
			connection.commit();
			statement = connection.prepareStatement(query2);
			rs = statement.executeQuery();
		} 
		catch (SQLException e1) {
			if(connection != null){
				System.out.println("transaction is rolled back");
				try {
					connection.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
					throw e;
				}
			}
			e1.printStackTrace();
			throw e1;
		}

		return rs;
	}


	/**
	 * Stores the Payment details of the payment made by cash
	 * Stores in cash and payment tables
	 * @param cash
	 */
	public ResultSet makeCashPayment(CashEntity cashEntity){
		System.out.println("insert cash service : ");
		ResultSet rs = null;
		String query = "INSERT INTO cash values (?,?,?)";

		try {
			connection.setAutoCommit(false);
			statement = connection.prepareStatement(query);
			statement.setInt(1,cashEntity.getPaymentId());
			statement.setDouble(2,cashEntity.getToatlAmount());
			statement.setDouble(3, cashEntity.getGivenAmount());
			statement.executeUpdate();
			connection.commit();

		} 
		catch (SQLException e1) {
			if(connection != null){
				System.out.println("transaction is rolled back");
				try {
					connection.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			e1.printStackTrace();
		}
		finally{
			try {
				connection.setAutoCommit(true);
			}
			catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return rs;
	} 




	/**
	 * @param creditCard
	 * @throws SQLException 
	 */
	public void insertIntoCreditCard(CreditCardEntity creditCard) throws SQLException {
		String query = "INSERT INTO creditcard values(?,?,?,?)";
		try {
			connection.setAutoCommit(false);
			statement = connection.prepareStatement(query);
			statement.setInt(1, creditCard.getPaymentId());
			statement.setString(2, creditCard.getCardNo());
			statement.setInt(3, creditCard.getExpiryDate());
			statement.setString(4, creditCard.getCardType());
			statement.executeUpdate();
		} 
		catch (SQLException e1) {
			if(connection != null){
				System.out.println("transaction is rolled back");
				try {
					connection.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
					throw e;
				}
			}
			e1.printStackTrace();
			throw e1;
		}
	}

	public void transactionCommit() throws SQLException{
		if(connection!=null){
			try {
				connection.commit();
			} catch (SQLException e) {
				if(connection != null){
					System.out.println("transaction is rolled back");
					try {
						connection.rollback();
					} catch (SQLException e1) {
						e1.printStackTrace();
						throw e1;
					}
				}
				e.printStackTrace();
				throw e;
			}
			finally{
				try {
					connection.setAutoCommit(true);
				}
				catch (SQLException e) {
					e.printStackTrace();
					throw e;
				}
			}
		}
	}

	public void transactionRollBack() throws SQLException{

		try {
			if(connection != null){
				connection.rollback();
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
			throw e1;
		}

		finally{
			try {
				connection.setAutoCommit(true);
			}
			catch (SQLException e) {
				e.printStackTrace();
				throw e;
			}
		}
	}

	public void closeConnection(){
		try{
			if(statement != null){
				statement.close();
			}
			if(connection != null){
				connection.close();
			}
		}
		catch(SQLException sqlExec){
			sqlExec.printStackTrace();
		}
	}
}
